/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.states;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import etherblocks.engine.*;
import etherblocks.engine.exceptions.*;
import etherblocks.engine.network.*;
import etherblocks.game.*;
import etherblocks.game.blocks.*;
import etherblocks.mods.Mod;
import com.cubes.*;

/**
 *
 * @author Carl
 */
public class NetworkAppState extends BaseAppState{

    public NetworkAppState(NetworkConnectionSettings connectionSettings){
        this.connectionSettings = connectionSettings;
    }
    private NetworkConnectionSettings connectionSettings;
    private NetworkClient networkClient;

    @Override
    public void initialize(AppStateManager stateManager, Application application){
        super.initialize(stateManager, application);
        System.out.println(connectionSettings.getUserType());
        switch(connectionSettings.getUserType()){
            case SERVER:
                try{
                    networkClient = new NetworkServer(connectionSettings.getPort());
                }catch(ServerCreationException ex){
                    System.out.println(ex.getMessage());
                    System.exit(0);
                }
                break;
            
            case CLIENT:
                try{
                    networkClient = new NetworkClient();
                    networkClient.connectToServer(connectionSettings.getHost(), connectionSettings.getPort());
                }catch(ServerConnectionException ex){
                    System.out.println(ex.getMessage());
                    System.exit(0);
                }catch(ServerConnectionTimeoutException ex){
                    System.out.println(ex.getMessage());
                    System.exit(0);
                }
                break;
            
            case SPECTATOR:
                System.out.println(new ConnectionModeNotAllowedException().getMessage());
                System.exit(0);
                break;
        }
        Mod mod = getAppState(ModAppState.class).getMod();
        GameClient gameClient = mod.createGameClient();
        gameClient.setNetworkClient(networkClient);
        gameClient.setMainApplication(mainApplication);
        networkClient.setGameClient(gameClient);
        if(networkClient.isServer()){
            World world;
            if(true){
                world = getAppState(WorldFileHandlerAppState.class).loadFile_DefaultDirectory("test");
            }
            else{
                world = new World();
                BlockTerrainControl blockTerrain = new BlockTerrainControl(CubesConfiguration.getSettings(mainApplication), new Vector3Int(7, 1, 7));
                blockTerrain.setBlocksFromNoise(new Vector3Int(), new Vector3Int(100, 30, 100), 0.5f, Block_Grass.class);
                blockTerrain.setBlockArea(new Vector3Int(), new Vector3Int(100, 1, 100), Block_Bedrock.class);
                world.setBlockTerrain(blockTerrain);
            }
            NetworkServer networkServer = (NetworkServer) networkClient;
            GameServer gameServer = mod.createGameServer();
            gameServer.setNetworkServer(networkServer);
            gameServer.setWorld(world);
            networkServer.setGameServer(gameServer);
        }
    }

    @Override
    public void cleanup(){
        super.cleanup();
        if(networkClient.isServer()){
            NetworkServer networkServer = (NetworkServer) networkClient;
            World world = networkServer.getGameServer().getWorld();
            getAppState(WorldFileHandlerAppState.class).saveFile_DefaultDirectory("test", world);
        }
    }

    public NetworkClient getNetworkClient(){
        return networkClient;
    }
}
